<template>
  <div class="infoWindow">
    <div class="header-content">
      <div class="flex">
        <svg-icon class="mr-10" name="position" :color="carStatusOption?.color" size="14" />
        <div>
          {{ carInfo.address }}
          <br />
          {{ carInfo.coordinate }}
        </div>
      </div>

      <div class="car-status" :style="{ color: carStatusOption?.color }">【{{ carStatusOption?.label }}】</div>

      <el-icon class="header-close" @click="handleClose"><ep-Close /></el-icon>
    </div>
    <div class="main-content">
      <div class="info-box mb-10">
        <el-row>
          <el-col :span="12" class="!flex">
            <div class="label">车牌号码：</div>
            {{ carInfo.mixingTruckNumber }}-{{ carInfo.carNumber }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">定位时间：</div>
            {{ carInfo.positionTime || '--' }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">车速：</div>
            {{ carInfo.speed ? `${carInfo.speed} KM/H` : '--' }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">方向角：</div>
            {{ carInfo.directionAngle ? `${carInfo.directionAngle}` : '--' }}
          </el-col>
        </el-row>
      </div>

      <div v-if="carInfo.carStatus === VehicleStatusEnum.InTransit" class="info-box">
        <el-row>
          <el-col :span="12" class="!flex">
            <div class="label">当前运单：</div>
            {{ carInfo.wayBillNumber || '--' }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">实际方量：</div>
            {{ carInfo.actualVolume ?? '--' }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">客户名称：</div>
            {{ carInfo.customerName || '--' }}
          </el-col>
          <el-col :span="12" class="!flex">
            <div class="label">开始送货时间：</div>
            {{ carInfo.deliveryStartTime || '--' }}
          </el-col>
        </el-row>
      </div>

      <div class="bottom-btn">
        <el-button type="success" round class="mr-10" @click="handleToVehicleTrackPlayback">轨迹回放</el-button>
        <!-- <el-button type="warning" round class="mr-10" @click="() => emit('trackVehicle', isTracking ? '' : props.carInfo.carId)">
          {{ isTracking ? '停止跟踪' : '跟踪车辆' }}
        </el-button> -->
        <el-button v-if="carInfo.carStatus === VehicleStatusEnum.InTransit" type="primary" round @click="handleToWaybillDetail">
          运单详情
        </el-button>
      </div>
    </div>

    <div class="arrows"></div>
  </div>
</template>

<script lang="ts" setup>
import { statusOptions, type StatusOptions } from '../options'
import { VehicleStatusEnum } from '../enums'
import type { CarInfoData } from '@/api/vehicle-monitor/vehicleMonitor'
import { routerNewWindow } from '@/utils/router'

const props = defineProps<{ carInfo: CarInfoData; trackCardId?: string }>()

const emit = defineEmits(['close', 'openWaybillDetail', 'trackVehicle'])

// 判断当前车辆是否正在跟踪
// const isTracking = computed(() => props.trackCardId === props.carInfo.carId)

const handleClose = () => {
  emit('close')
}

const carStatusOption: ComputedRef<StatusOptions> | undefined = computed(() =>
  statusOptions.find(option => option.value === props.carInfo.carStatus)
)

const handleToVehicleTrackPlayback = () => {
  routerNewWindow({
    name: 'VehicleTrack',
    query: {
      carNumber: props.carInfo.carNumber,
      deviceId: props.carInfo.deviceId
    }
  })
}

const handleToWaybillDetail = () => {
  emit('openWaybillDetail')
}
</script>
<style lang="scss" scoped>
.infoWindow {
  position: relative;
  width: 463px;
  border-radius: 6px;
  padding: 10px;
  box-sizing: border-box;
  background: var(--el-bg-color-overlay);
  font-size: var(--el-font-size-extra-small);
  .header-content {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--el-border-color);
    box-sizing: border-box;
    .nav-icon {
      display: inline-block;
      width: 16px;
      height: 16px;
      margin-right: 10px;
    }
    .car-status {
      min-width: 55px;
      margin-right: 10px;
      display: flex;
      align-items: flex-end;
      flex-direction: column;
    }
  }
  .header-close {
    cursor: pointer;
    position: absolute;
    top: 10px;
    right: 10px;
  }

  .main-content {
    padding-top: 10px;
    .info-box {
      padding-bottom: 5px;
      border-bottom: 1px solid var(--el-border-color);
      :deep(.el-col) {
        padding-bottom: 5px;
        padding-left: 5px;
      }
      .label {
        min-width: 60px;
        font-weight: 600;
      }
    }
  }
}

.bottom-btn {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  .el-button {
    width: 125px;
  }
}

.arrows {
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-left-width: 8px;
  border-right-width: 8px;
  border-top-width: 10px;
  border-top-color: var(--el-bg-color-overlay);
  position: absolute;
  left: 50%;
  bottom: -14px;
  transform: translateX(-50%);
}
</style>
